var exec = require('child_process').exec;
var readline = require('readline');
var iconv = require('iconv-lite')
var stream = require('stream')
var Transform = stream.Transform;
var filepath = process.argv[2];
console.log(filepath);
var cmd = 'tail -500f ' + filepath;
var child = exec(cmd, function(err, stdout, stderr){
	console.log('err ' + err)
//	console.log('stdout ' + stdout)
	console.log('stderr ' + stderr)
})

function GbkTransformer(options) {
  if (!(this instanceof GbkTransformer))
    return new GbkTransformer(options);

  Transform.call(this, options);
}

GbkTransformer.prototype = Object.create(
		  Transform.prototype, { constructor: { value: GbkTransformer }});

GbkTransformer.prototype._transform = function(chunk, encoding, done){
	var decodechunk = iconv.decode(chunk, 'gbk');
	this.push(decodechunk);
	done();
}

var transformer = new GbkTransformer();
child.stdout.pipe(transformer)

var lineReader = readline.createInterface({
	input : transformer,
	output : new stream.Writable()
});
lineReader.on('line', function(line){
//	console.log(line)
})
//
//lineReader.on('line', function(line){
//	console.log(typeof line)
//});

//child.stdout.on('data', function(data){
//	console.log(iconv.decode(data, 'gbk'))
//})